//
//  UIView+SJAdd.swift
//  SJKit
//
//  Created by 刘洋 on 2016/12/30.
//  Copyright © 2016年 刘洋. All rights reserved.
//

import UIKit

// MARK: - 布局

extension SwordJoy where Base: UIView {
  var origin: CGPoint {
    get { return base.frame.origin }
    set { base.frame.origin = newValue }
  }
  
  var left: CGFloat {
    get { return base.frame.origin.x }
    set { base.frame.origin.x = newValue }
  }
  
  var top: CGFloat {
    get { return base.frame.origin.y }
    set { base.frame.origin.y = newValue }
  }
  
  var right: CGFloat {
    get { return base.frame.origin.x + base.frame.size.width }
    set { base.frame.origin.x = newValue - base.frame.size.width }
  }
  
  var bottom: CGFloat {
    get { return base.frame.origin.y + base.frame.size.height }
    set { base.frame.origin.y = newValue - base.frame.size.height }
  }
  
  var height: CGFloat {
    get { return base.frame.size.height }
    set { base.frame.size.height = newValue }
  }
  
  var width: CGFloat {
    get { return base.frame.size.width }
    set { base.frame.size.width = newValue }
  }
  
  var center: CGPoint {
    get { return base.center }
    set { base.center = newValue }
  }
  
  var centerX: CGFloat {
    get { return base.center.x }
    set { base.center = CGPoint(x: newValue, y: base.center.y) }
  }
  
  var centerY: CGFloat {
    get { return base.center.y }
    set { base.center = CGPoint(x: base.center.x, y: newValue) }
  }
  
  var size: CGSize {
    get { return base.frame.size }
    set { base.frame.size = newValue }
  }
}

// MARK: - 截图

extension SwordJoy where Base: UIView {
  /// 截图
  ///
  /// - Returns: 截出来的图
  func snapshotImage(_ afterScreenUpdates: Bool = false) -> UIImage {
    UIGraphicsBeginImageContextWithOptions(base.bounds.size, base.isOpaque, 0)
    base.drawHierarchy(in: base.bounds, afterScreenUpdates: afterScreenUpdates)
    let snap = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return snap!
  }
}

extension SwordJoy where Base: UIView  {
  
  // 用来判断该视图或者该视图的子视图中是否正在滑动
  // 应用场景: UIPickerView滑动的时候点击确定会导致数据不正确
    var isScrolling: Bool {
        if let scrollView = base as? UIScrollView {
            if (scrollView.isDragging || scrollView.isDecelerating) {
                return true
            }
        }

        for subview in base.subviews {
            if ( subview.sj.isScrolling ) {
                return true
            }
        }
        return false
    }
    
    func removeFromeSuperViewIfExist() {
        if nil != base.superview {
            base.removeFromSuperview()
        }
    }
}
extension UIView  {
    
    // 用来判断该视图或者该视图的子视图中是否正在滑动
    // 应用场景: UIPickerView滑动的时候点击确定会导致数据不正确
    var isScrolling: Bool {
        if let scrollView = self as? UIScrollView {
            if (scrollView.isDragging || scrollView.isDecelerating) { return true }
        }
        
        for subview in self.subviews {
            if ( subview.isScrolling ) { return true }
        }
        
        return false
    }
    
}
